package com.lib76.buildCode.control.pageConfig;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lib76.buildCode.dao.ProjectDao;
import com.lib76.buildCode.dto.AjaxResult;
import com.lib76.buildCode.dto.TableResult;
import com.lib76.buildCode.dto.TreeNode;
import com.lib76.buildCode.model.Menu;
import com.lib76.buildCode.model.PageConfig;
import com.lib76.buildCode.model.Project;
import com.lib76.buildCode.service.MenuConfigServer;
import com.lib76.jsp.HttpServletUtils;

/**
 * 菜单配置页面控制
 * @author 张浩
 * @version 0.0.4
 */
@Controller
public class MenuController {

	/**
	 * 访问菜单配置页面
	 * @param pcode
	 * @param model
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/config")
	public String config(String pcode,Model model){
		if(StringUtils.isNotBlank(pcode)){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null){
				model.addAttribute("project",project);
				return "pageConfig/menu/list";
			}
		}
		return HttpServletUtils.sendRedirect("/");
	}
	
	/**
	 * 菜单表格数据
	 * @param pcode
	 * @param pid
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/data")
	@ResponseBody
	public TableResult<Menu> data(String pcode,String pid){
		TableResult<Menu> result = new TableResult<Menu>();
		if(StringUtils.isNotBlank(pcode)){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null && project.getPageConfig() != null){
				if(StringUtils.isNotBlank(pid)){
					Menu menu = MenuConfigServer.findMenu(project.getPageConfig().getMenuConfig(), pid);
					if(menu != null && menu.getChildren() != null){
						result.setData(new ArrayList<Menu>(menu.getChildren().values()));
					}else{
						result.setData(new ArrayList<Menu>());
					}
				}else{
					result.setData(new ArrayList<Menu>(project.getPageConfig().getMenuConfig().values()));
				}
			}
		}
		return result;
	}
	
	/**
	 * 菜单树节点数据
	 * @param pcode
	 * @param pid
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/tree")
	@ResponseBody
	public List<TreeNode> tree(String pcode,String pid){
		List<TreeNode> result = new ArrayList<TreeNode>();
		if(StringUtils.isNotBlank(pcode)){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null && project.getPageConfig() != null){
				PageConfig pageConfig = project.getPageConfig();
				if(StringUtils.isNotBlank(pid)){
					Menu parentMenu = MenuConfigServer.findMenu(pageConfig.getMenuConfig(),pid);
					if(parentMenu != null && parentMenu.getChildren()!=null){
						for(Menu menu:parentMenu.getChildren().values()){
							TreeNode node = new TreeNode();
							node.setId(menu.getId());
							node.setName(menu.getName());
							node.setPid(menu.getParent());
							node.setIsLeaf(menu.getChildren()==null);
							result.add(node);
						}
					}
				}else{
					for(Menu menu:pageConfig.getMenuConfig().values()){
						TreeNode node = new TreeNode();
						node.setId(menu.getId());
						node.setName(menu.getName());
						node.setPid(menu.getParent());
						node.setIsLeaf(menu.getChildren()==null);
						result.add(node);
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * 访问菜单编辑页面
	 * @param pcode
	 * @param pid
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/edit")
	public String edit(String pcode,String pid,String id,Model model){
		if(StringUtils.isNotBlank(pcode)){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null && project.getPageConfig() != null){
				Menu menu = null;
				if(StringUtils.isNotBlank(id)){
					menu = MenuConfigServer.findMenu(project.getPageConfig().getMenuConfig(), id);
				}else{
					menu = new Menu();
					menu.setParent(pid);
				}
				model.addAttribute("project",project);
				model.addAttribute("menu",menu);
				return "pageConfig/menu/edit";
			}
		}
		return HttpServletUtils.sendRedirect("/");
	}
	/**
	 * 移动某个菜单到指定菜单下
	 * @param pcode
	 * @param srcId
	 * @param targetId
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/move")
	@ResponseBody
	public AjaxResult move(String pcode,String srcId,String targetId){
		AjaxResult result = new AjaxResult(0);
		if(StringUtils.isNotBlank(pcode) && srcId != targetId){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null && project.getPageConfig() != null){
				if(MenuConfigServer.moveTo(project.getPageConfig().getMenuConfig(), srcId, targetId)){
					result.setError(1);
				}else{result.setMsg("移动失败");}
			}else{result.setMsg("未找到项目或项目不支持页面配置");}
		}else{result.setMsg("无效的参数");}
		return result;
	}
	
	/**
	 * 保存或修改菜单
	 * @param pcode
	 * @param menu
	 * @return
	 */
	@RequestMapping("/pageConfig/menu/save")
	@ResponseBody
	public AjaxResult save(String pcode,String id,Menu menu){
		AjaxResult result = new AjaxResult(0);
		if(StringUtils.isNotBlank(pcode)){
			Project project = ProjectDao.findByCode(pcode);
			if(project != null && project.getPageConfig() != null){
				if(StringUtils.isNotBlank(id)){
					Menu entity =  MenuConfigServer.findMenu(project.getPageConfig().getMenuConfig(),id);
					if(entity != null){
						entity.setCode(menu.getCode());
						entity.setName(menu.getName());
						entity.setUrl(menu.getUrl());
						result.setData(entity);
						result.setError(1);
					}else{result.setMsg("未找到修改的对象");}
				}else if(StringUtils.isBlank(menu.getParent())){
					menu.setParent("root");
					project.getPageConfig().getMenuConfig().put(menu.getCode(), menu);
					result.setData(menu);
					result.setError(1);
				}else{
					Menu perentMenu = MenuConfigServer.findMenu(project.getPageConfig().getMenuConfig(),menu.getParent());
					if(perentMenu != null){
						if(perentMenu.getChildren() == null){
							perentMenu.setChildren(new LinkedHashMap<String,Menu>());
						}
						perentMenu.getChildren().put(menu.getCode(), menu);
						result.setData(menu);
						result.setError(1);
					}else{result.setMsg("未找到父菜单配置");}
				}
				
				
			}else{result.setMsg("未找到项目或项目不支持页面配置");}
		}else{result.setMsg("无效的参数");}
		return result;
	}
}
